﻿using HCQ2_Common;
using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.APIBase;
using HCQ2_Model.VRQDMeta.InputDto._20220112;
using HCQ2_Model.VRQDMeta.OutputDto._20220112;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HCQ2_BLL
{
    public partial class T_DocumentInfoLogBLL: IT_DocumentInfoLogBLL 
    {
        /// <summary>
        ///  获取待审核应用列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BasePage<AppPendingListOutput> GetMetaAppPendingList(AppPendingListInput param,int user_id)
        {
            BasePage<AppPendingListOutput> result = new BasePage<AppPendingListOutput>();
            int total;
            var list = DBSession.IT_DocumentInfoLogDAL.GetMetaAppPendingList(param, user_id, out total);
            result.total = total;
            list?.ForEach(o =>
            {
                //头像
                if (!string.IsNullOrEmpty(o.createImage))
                    o.createImage = o.createImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //价格详情
                List<PriceDetial> listPrice = new List<PriceDetial>();
                if (!string.IsNullOrEmpty(o.prices))
                {
                    string[] prices = o.prices.Split(',');
                    foreach(var item in prices)
                    {
                        string[] price = item.Split(':');
                        listPrice.Add(new PriceDetial { month = Helper.ToInt(price[0]), price = Helper.ToDecimal(price[1]) });
                    }
                }
                o.listPrice = listPrice;
                //时长
                o.timeSpan = (o.finish_time > 0) ? Math.Round((decimal)o.finish_time / 60 / 60, 2) : 0;
            });
            result.rows = list;
            return result;
        }
        /// <summary>
        ///  获取应用商品详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public MetaAppDetailOutput GetMetaAppDetail(MetaAppDetailInput param, int user_id)
        {
            var obj = DBSession.IT_DocumentInfoLogDAL.GetMetaAppDetail(param, user_id);
            if (obj == null)
                return null;
            //头像
            if (!string.IsNullOrEmpty(obj.createImage))
                obj.createImage = obj.createImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            //时长
            obj.timeSpan = (obj.finish_time > 0) ? Math.Round((decimal)obj.finish_time / 60 / 60, 2) : 0;
            //价格详情
            List<MateAppPriceListModel> listPrice = new List<MateAppPriceListModel>();
            List<T_VRGoodDisiPrice> PriceList = DBSession.IT_VRGoodDisiPriceDAL.Select(s => s.file_id == obj.fileID);
            PriceList?.ForEach(s => listPrice.Add(new MateAppPriceListModel { month = s.op_month, price = s.ori_price }));
            obj.listPrice = listPrice;
            return obj;
        }
        /// <summary>
        ///  获取应用版本详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public MetaAppVersionOutput GetMetaAppVersionDetail(MetaAppVersionInput param)
        {
            var obj = DBSession.IT_DocumentInfoLogDAL.GetMetaAppVersionDetail(param);
            if (obj == null)
                return null;
            //头像
            if (!string.IsNullOrEmpty(obj.createImage))
                obj.createImage = obj.createImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            //价格详情
            if (!string.IsNullOrEmpty(obj.prices))
            {
                List<MateAppPriceListModel> listPrice = new List<MateAppPriceListModel>();
                string[] prices = obj.prices.Split(',');
                foreach (var item in prices)
                {
                    string[] price = item.Split(':');
                    listPrice.Add(new MateAppPriceListModel { month = Helper.ToInt(price[0]), price = Helper.ToDecimal(price[1]) });
                }
                obj.listPrice = listPrice;
            }
            return obj;
        }
        /// <summary>
        ///  获取应用版本历史列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<MetaAppVersionListOutput> GetMetaAppVersionList(MetaAppVersionListInput param)
        {
            var obj = DBSession.IT_DocumentInfoLogDAL.GetMetaAppVersionList(param);
            obj?.ForEach(s =>
            {
                //价格详情
                if (!string.IsNullOrEmpty(s.prices))
                {
                    List<MateAppPriceListModel> listPrice = new List<MateAppPriceListModel>();
                    string[] prices = s.prices.Split(',');
                    foreach (var item in prices)
                    {
                        string[] price = item.Split(':');
                        listPrice.Add(new MateAppPriceListModel { month = Helper.ToInt(price[0]), price = Helper.ToDecimal(price[1]) });
                    }
                    s.listPrice = listPrice;
                }
            });
            return obj;
        }
    }
}